package com.example.composableexample.project.meditation

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddAPhoto
import androidx.compose.material.icons.filled.Details
import androidx.compose.material.icons.filled.Devices
import androidx.compose.material.icons.filled.Dialpad
import androidx.compose.material.icons.filled.Dining
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composableexample.R

@Composable
fun AppTitle() {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 16.dp),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            text = stringResource(id = R.string.silent),
            style = MaterialTheme.typography.titleLarge,
            fontSize = 16.sp,
            textAlign = TextAlign.Center,
            color = Color.Blue
        )
        Spacer(modifier = Modifier.width(12.dp))
        Image(painter = painterResource(id = R.drawable.ic_woof_logo), contentDescription = "logo")
        Spacer(modifier = Modifier.width(12.dp))
        Text(
            text = stringResource(id = R.string.moon),
            style = MaterialTheme.typography.titleLarge,
            fontSize = 16.sp,
            color = Color.Blue
        )
    }
}

@Composable
fun GreetingSection(name: String = "Your name") {
    Column(modifier = Modifier.padding(horizontal = 16.dp)) {
        Text(
            text = stringResource(id = R.string.good_morning) + " $name",
            style = MaterialTheme.typography.titleLarge,
            fontSize = 22.sp
        )
        Text(
            text = stringResource(id = R.string.wishing_desc),
            style = MaterialTheme.typography.titleLarge,
            fontSize = 18.sp
        )
    }
}

@Composable
fun CategoryItem(category: Categories, selectedItem: Boolean, onItemClick: () -> Unit) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.padding(end = 15.dp)
    ) {
        Box(
            modifier = Modifier
                .clip(RoundedCornerShape(18.dp))
                .background(color = if (selectedItem) Color.Blue else Color.Gray)
                .clickable { onItemClick() }
        ) {
            Icon(
                imageVector = category.icon,
                contentDescription = "Category Icon",
                tint = Color.White,
                modifier = Modifier
                    .padding(18.dp)
                    .size(20.dp)
            )
        }
        Spacer(modifier = Modifier.height(5.dp))
        Text(
            text = category.title,
            style = MaterialTheme.typography.bodyLarge,
            fontSize = 12.sp,
            color = if (selectedItem) Color.Blue else Color.Gray,
            fontWeight = FontWeight.Bold
        )
    }
}

@Composable
fun CategoriesSelection(categoriesList: List<Categories>, initialSelectionItemIndex: Int = 0) {
    var selectedItemIndex by remember {
        mutableStateOf(initialSelectionItemIndex)
    }
    LazyRow(modifier = Modifier.padding(start = 16.dp)) {
        items(categoriesList.size) {
            CategoryItem(category = categoriesList[it], selectedItem = selectedItemIndex == it) {
                selectedItemIndex = it
            }
        }
    }
}

data class Categories(val icon: ImageVector, val title: String)

data class Feature(val image: Int, val title: String, val description: String, val time: String)

@Composable
fun FeatureItem(feature: Feature, modifier: Modifier = Modifier, textColor: Color) {
    BoxWithConstraints(modifier = modifier) {
        val width = constraints.maxWidth
        val height = constraints.maxHeight
        Box(modifier = Modifier.fillMaxSize()) {
            Image(
                painter = painterResource(id = feature.image),
                contentDescription = feature.title,
                modifier = Modifier.align(Alignment.TopEnd)
            )
            Column(
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.Start,
                modifier = Modifier
                    .fillMaxHeight()
                    .padding(start = 16.dp)
            ) {
                Text(
                    text = feature.title,
                    style = MaterialTheme.typography.bodySmall,
                    color = textColor,
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold
                )
                Text(
                    text = feature.description,
                    color = textColor,
                    style = MaterialTheme.typography.bodyLarge,
                    fontSize = 14.sp
                )
            }
            Row(
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.Bottom,
                modifier = Modifier
                    .fillMaxWidth()
                    .fillMaxHeight()
                    .padding(bottom = 16.dp, start = 16.dp, end = 16.dp)
            ) {
                Text(
                    text = feature.time,
                    color = textColor,
                    fontSize = 12.sp,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.padding(bottom = 6.dp)
                )
                Text(
                    text = "Start",
                    color = Color.White,
                    fontSize = 10.sp,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .clickable { }
                        .clip(RoundedCornerShape(122.dp))
                        .background(color = Color.Blue)
                        .padding(vertical = 6.dp, horizontal = 16.dp)
                )
            }
        }
    }
}

@Preview
@Composable
fun AppTitlePreview() {
    AppTitle()
}

@Preview
@Composable
fun GreetingSectionPreview() {
    GreetingSection(name = "Java")
}

@Preview
@Composable
fun CategoryItemPreview() {
    val categories = Categories(icon = Icons.Default.AddAPhoto, title = "Kotlin")
//    CategoryItem(category = categories, selectedItem = true, onItemClick = {})
    CategoryItem(category = categories, selectedItem = false, onItemClick = {})
}

@Preview
@Composable
fun CategoriesSelectionPreview() {
    val categoriesList = listOf(
        Categories(icon = Icons.Default.Dining, title = "Android"),
        Categories(icon = Icons.Default.Dialpad, title = "Kotlin"),
        Categories(icon = Icons.Default.Devices, title = "Java"),
        Categories(icon = Icons.Default.Details, title = "Compose")
    )
    CategoriesSelection(categoriesList = categoriesList)
}

@Preview
@Composable
fun FeatureItemPreview() {
    val feature = Feature(
        image = R.drawable.bg1,
        title = "Java",
        description = "描述手动阀手动阀",
        time = "2022-11-15"
    )
    FeatureItem(
        feature = feature,
        modifier = Modifier.size(width = 200.dp, height = 300.dp).clip(RoundedCornerShape(16.dp)).background(color = Color(0xFF2979FF)),
        textColor = Color.Magenta
    )
}